package Tree;

/**
 * @BelongsProject: SeniorArchitect-LeetCode
 * @BelongsPackage: Tree
 * @Author: zhuangxiaoyan
 * @CreateTime: 2023-10-29  17:02
 * @Description: TODO
 * @Version: 1.0
 */
public class 二叉搜索树的目标节点700 {

    public TreeNode searchBST(TreeNode root, int val) {
        if (root == null) {
            return root;
        }
        if (root.val == val) {
            return root;
        }
        if (root.val > val) {
            TreeNode left = searchBST(root.left, val);
            if (left != null) {
                return left;
            }
        }
        if (root.val < val) {
            TreeNode right = searchBST(root.right, val);
            if (right != null) {
                return right;
            }
        }
        return null;
    }

    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
}
